package com.coins.agent.zb;

import java.util.Observable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coins.agent.zb.util.ZBUtils;
import com.coins.common.jobs.JobMessage;
import com.coins.common.utils.BaseObserver;

/**
 * 功能：接收(新)获取多个委托买单或卖单，每次请求返回pageSize<100条记录
 * 
 * @author 任梁荣
 */
public class ZBGetOrdersNewOnWorker implements BaseObserver {

	protected static final Logger LOGGER = LoggerFactory.getLogger(ZBGetOrdersNewOnWorker.class);

	@Override
	public void update(Observable o, Object arg) {
		Object[] args = (Object[]) arg;
		if (args != null && args.length > 0) {
			if (args[0] instanceof JobMessage) {
				JobMessage msg = (JobMessage) args[0];
				JSONArray arrays = (JSONArray) msg.getData();
				if (arrays != null && arrays.size() > 0) {
					JSONObject json = arrays.getJSONObject(1);
					String access = json.getString("access");
					String secret = json.getString("secret");
					String coinName = json.getString("coinName");
					String pageSize = json.getString("pageSize");
					String tradeType = json.getString("tradeType");
					String pageIndex = json.getString("pageIndex");
					try {
						String result = ZBUtils.getOrdersNew(coinName, pageIndex, pageSize, tradeType, access, secret);
						msg.setResult(result);
						msg.setData(null);
						LOGGER.info("请求URL[" + msg.getEvents() + "][coin = " + coinName + "]接收到[" + result.length() + "]个字节");
					} catch (Exception e) {
						e.printStackTrace();
						if (LOGGER.isErrorEnabled())
							LOGGER.error(String.format("获取委托单数据失败[coinName = %s,e = %s]", coinName, e.getMessage()));
					}
				}
			}
		}
	}
}
